#!/usr/bin/env -S node -r ./livescript-transform-implicit-async/register


require! <[
  file-trace
  path
  favicons
  sharp
]>


module.exports = (cwd)!~>
  file = file-trace cwd
  trace = await file.trace!

  if await trace.cached()
    return

  # https://github.com/lovell/sharp/issues/1593
  # HACK FOR : Intermittent Error: Input file has corrupt header: glib: SVG has no elements
  try
    await sharp(
      Buffer.from(
        """<svg xmlns="http://www.w3.org/2000/svg"><rect width="1" height="1"/></svg>""",
        'utf-8'
      )
    ).metadata()
  catch err
    console.trace!
    console.log err

  console.log '生成图标中'
  {source, config} = await trace.read-yaml(\6du/ico)

  await favicons(
    path.join(cwd, source)
    config
    (error, response)~>>
      if error
        console.error error.message
      li = []
      for key in ['images','files']
        for {name,contents} in response[key]
          li.push name
          await trace.write(name, contents)
      console.log "成功生成 : "+li.join ' '
      await trace.cache(source)
      await trace.save()
  )
